package com.wnhz.work.yz.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.EntiretyBathroomAdapter;
import com.wnhz.work.yz.bean.BathBean;
import com.wnhz.work.yz.bean.EntiretyBathroomBean;
import com.wnhz.work.yz.bean.Finish1;
import com.wnhz.work.yz.fragment.EntiretyBathroomFragment;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.ZoomOutPageTransformer;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---整体卫浴
 */
public class EntiretyBathroomActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private EntiretyBathroomAdapter mAdapter;
    private ViewPager vp_bathroom;

    private ImageView[] indicators;
    private ArrayList<Fragment> lists;
    private LinearLayout indicatorLayout;

    private List<EntiretyBathroomBean> beanList = new ArrayList<>();
    private List<BathBean> bathList = new ArrayList<>();

    private TextView tv_title;
    private TextView tv_content1;
    private TextView tv_content2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_entirety_bathroom);
        EventBus.getDefault().register(this);

        initData();
        initTitle();
        initView();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Finish1 event) {
        finish();
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);//反注册EventBus
        super.onDestroy();
    }

    private void initData() {

    }

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("整体卫浴");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    private void initView() {
        vp_bathroom = (ViewPager) findViewById(R.id.vp_bathroom);
        indicatorLayout = (LinearLayout) findViewById(R.id.bathroom_indicator);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_content1 = (TextView) findViewById(R.id.tv_content1);
        tv_content2 = (TextView) findViewById(R.id.tv_content2);

        mAdapter = new EntiretyBathroomAdapter(getSupportFragmentManager());
        upload();
    }

    //获取数据
    private void upload() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("sign", "ztys");
        XUtil.Post(Url.PACKAGE_GETPACKAGECATELIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                super.onSuccess(result);
                System.out.println("sign====== " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    EntiretyBathroomBean bean = new EntiretyBathroomBean();
                    Gson gson = new Gson();
                    bean = gson.fromJson(jsonObject.optString("info"), EntiretyBathroomBean.class);
                    beanList.add(bean);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                System.out.println("ztys====  " + beanList.size());
                for (int i = 0; i < beanList.size(); i++) {
                    System.out.println("ztys====  " + beanList.get(i).getData().size());
                    bathList.addAll(beanList.get(i).getData());
                    System.out.println("ztys====  " + bathList.size());
                }

                lists = new ArrayList<>();
                for (int i = 0; i < bathList.size(); i++) {
                    EntiretyBathroomFragment fragment = new EntiretyBathroomFragment();
                    fragment.setBath(bathList.get(i));
                    lists.add(fragment);
                }

                tv_content1.setText(bathList.get(0).getDescribe1());
                mAdapter.setData(lists);
                mAdapter.notifyDataSetChanged();
                vp_bathroom.setAdapter(mAdapter);
                vp_bathroom.setOffscreenPageLimit(3);
                vp_bathroom.setPageTransformer(true, new ZoomOutPageTransformer());
                vp_bathroom.setCurrentItem(lists.size() / 2, false);
                initViewPager();

            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    //点击返回
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            default:
                break;
        }
    }

    //初始化viepager，根据数据的size加载几个Imageview
    private void initViewPager() {
        int indicatorSize = lists.size();

        indicators = new ImageView[indicatorSize];
        for (int i = 0; i < indicators.length; i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.view_indicator, null);
            indicators[i] = (ImageView) view.findViewById(R.id.image_indicator);
            LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(15,15);
            layoutParams.setMargins(5,0,5,0);
            indicators[i].setLayoutParams(layoutParams);
            indicatorLayout.addView(view);
        }


        setViewPager();
    }

    //viewpager改变时切换页面
    private void setViewPager() {
        vp_bathroom.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                currentPosition = arg0;

                tv_title.setText(bathList.get(arg0).getTitle());
                tv_content1.setText(bathList.get(arg0).getDescribe1());
                tv_content2.setText(bathList.get(arg0).getDescribe2());

                setIndicator(currentPosition);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });


        currentPosition = 0;
        setIndicator(currentPosition);
        vp_bathroom.setCurrentItem(currentPosition, false);
        tv_title.setText(bathList.get(currentPosition).getTitle());
    }

    /**
     * 设置指示器小圆点的位置
     *
     * @param selectedPosition 默认指示器位置
     */
    private void setIndicator(int selectedPosition) {


        for (int i = 0; i < indicators.length; i++) {
            indicators[i].setBackgroundResource(R.drawable.icon_point);
        }
        if (indicators.length > selectedPosition)
            indicators[selectedPosition].setBackgroundResource(R.drawable.icon_point_pre);
    }

    private int currentPosition;

}
